<?php

namespace app\common\model\Project;
// 引入模型
use think\Model;

// 订单产品的模型
class Project extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'project';
    // 自动插入的时间戳字段
    protected $autoWriteTimestamp = true;
    //  创建时间字段
    protected $createTime = false;
    protected $updateTime = false;

    protected $append  = [
        'status_text',
        'thumb_text',
        'createtime_text', 
        'endtime_text',
        'count_text',
        'nums_text',
    ];

    public function getNumsTextAttr($value, $data)
    {
        $nums = isset($data['nums']) ? $data['nums'] : 0;

        $nums=$nums == 0 ? '不限人数' : $nums;
        // $count = model('Project.Order')->where(['pid' => $id])->count();

        return $nums;
    }

    public function getCountTextAttr($value, $data)
    {
        $id = isset($data['id']) ? $data['id'] : 0;

        $count = model('Project.Order')->where(['pid' => $id])->count();

        return $count;
    }

    public function getStatusTextAttr($value,$data)
    {
        $status = isset($data['status']) ? $data['status'] : '0';
        $status = empty($status) ? '0' : $status;

        switch ($status){
            case '0':
                return "未开始报名";
                break;
            case '1':
                return "开始报名";
                break;
            case '2':
                return "截止报名";
                break;
            case '3':
                return "已开始";
                break;
            case '4':
                return "已结束";
                break;
            default:
                return '未知状态';
        }
    }

    public function getThumbTextAttr($value, $data)
    {
        //域名地址
        $domian = empty(config('url_domain_root')) ? "http://www.fast.com/" : trim(config('url_domain_root'));
        $domian = trim($domian, "/");

        //给一张默认图片地址
        $default = '/assets/img/avatar.png';

        //拿出头像的字段信息
        $thumbs = isset($data['thumbs']) ? trim($data['thumbs']) : $default;
        $thumbs = is_file(".".$thumbs) ? $thumbs : $default;
        $thumbs = $domian.$thumbs;
        return $thumbs;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime = isset($data['createtime']) ? $data['createtime'] : '';

        if(empty($createtime))
        {
            return '';
        }
        
        return date('Y-m-d H:i', $createtime);
    }

    public function getEndtimeTextAttr($value, $data)
    {
        $endtime = isset($data['endtime']) ? $data['endtime'] : '';

        if(empty($endtime))
        {
            return '';
        }
        
        return date('Y-m-d H:i', $endtime);
    }
}
